package thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo38 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(20);

        //构造方法中的数字就是拆分出来的任务的个数
        CountDownLatch countDownLatch = new CountDownLatch(20);
        for (int i = 0; i <= 20; i++) {
            int id = i;
            executorService.submit(() -> {
                System.out.println("下载任务开始：" + id + " 开始执行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("下载任务结束: " + id + "结束执行");
                //完毕
                countDownLatch.countDown();
            });
        }
        //await就是all wait
        countDownLatch.await();
        System.out.println("所有任务都完成了");
    }
}
